package com.sq.partner.manager.util;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
/**
 * 请求WebService工具类
 * @author Mango
 *
 */
public class WebServiceUtil {

	/**
	 * 调用WebService
	 * @param reqXml
	 * @param url
	 * @param method
	 * @param namespace
	 * @return 响应内容
	 * @throws Exception
	 */
	public static String call(String reqXml,String url,String method,String namespace) throws Exception{
		// 调用过程
		Service service = new Service();
		
		Call call = (Call) service.createCall();
		call.setOperationName(new QName(namespace,method));//namespace命名空间，method方法名
		call.setTargetEndpointAddress(new URL(url));
		call.addParameter("xml",XMLType.SOAP_STRING,ParameterMode.IN);// 参数的类型
		call.setReturnType(XMLType.SOAP_STRING);// 设置返回类型
		call.setUseSOAPAction(true);// 给方法传递参数，并且调用方法
		
		Object[] obj = new Object[] { reqXml };
		Object response = call.invoke(obj);
			
		return response==null?"":response.toString();
	}
	
	public static void main(String[] args) throws Exception {
		String url = "http://10.196.1.3:7001/sales/services/SalesService?wsdl";
		String method = "queryAgentAndAgreeInfo";
		String namespace = "http://webservice.sales.sinosoft.com";
		String reqXml = 
					"<?xml version=\"1.0\" encoding=\"GBK\"?>" +
					"<XML>" +
						"<AGENT>" +
							"<COMCODE>13031600</COMCODE>" +
							"<TEAMCODE>13031601</TEAMCODE >" +
							"<BUSINESSSOURCE>A0102</BUSINESSSOURCE>" +
							"<CHANNELNAME>A</CHANNELNAME>" +
							"<PRODCODE>aaaa</PRODCODE>" +
							"<AGENTCODE></AGENTCODE>" +
							"<USERCODE></USERCODE>" +
						"</AGENT>" +
					"</XML>";

		String response = WebServiceUtil.call(reqXml, url, method, namespace);
		System.out.println(response);
	}
}
